//
//  TeamViewController.m
//  LOL_JewelBox
//
//  Created by dllo on 15/4/27.
//  Copyright (c) 2015年 dllo. All rights reserved.
//

#import "TeamViewController.h"
#import "AFHTTPRequestOperationManager.h"
#import "UIImageView+WebCache.h"
#import "TeamModel.h"
#import "ToolsView.h"
#import "TeamsCell.h"
#import "DetailTeamViewController.h"
@interface TeamViewController ()<UITableViewDataSource,UITableViewDelegate>

@property(nonatomic, retain)ToolsView *toolsView;
@property(nonatomic, retain)UITableView *teamTableView;
@property(nonatomic, retain)NSMutableArray *transformArr;
@property(nonatomic, retain)NSMutableArray *teamArr;

@end

@implementation TeamViewController


- (void)dealloc
{
    
    [_toolsView release];
    [_teamTableView release];
    [_transformArr release];
    [_teamArr release];
    [super dealloc];
    
}


- (instancetype)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        self.title = @"最佳阵容";
        self.teamArr = [NSMutableArray array];
        self.transformArr = [NSMutableArray array];
    }
    return self;
}

// 数据解析
- (void)teamData
{
    AFHTTPRequestOperationManager *managerTame = [AFHTTPRequestOperationManager manager];
    [managerTame GET:@"http://box.dwstatic.com/apiHeroBestGroup.php?v=70&OSType=iOS7.1.1" parameters:nil success:^(AFHTTPRequestOperation *operation, id responseObject) {
        
        NSMutableArray *arr = responseObject;
        for (NSMutableDictionary *dic in arr) {

            TeamModel *teamModel = [[TeamModel alloc] init];
            [teamModel setValuesForKeysWithDictionary:dic];
            teamModel.titel = dic[@"title"];
            [self.teamArr addObject:teamModel];
            [teamModel release];
            
        }
        
        [self.teamTableView  reloadData];
    } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
        
    }];
}


- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    [self teamData];
    
    // 铺设自定义View
    self.toolsView = [[ToolsView alloc] initWithFrame:CGRectMake(0, 0, VIEWWIDTH, VIEWHEIGHT)];
    self.toolsView.backgroundColor = [UIColor whiteColor];
    [self.view addSubview:self.toolsView];
    [_toolsView release];
    
    // 铺设tableview
    self.teamTableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, VIEWWIDTH, VIEWHEIGHT - 64 - 49) style:UITableViewStylePlain];
    self.teamTableView.delegate = self;
    self.teamTableView.dataSource = self;
    self.teamTableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    [self.toolsView addSubview:self.teamTableView];
    [_teamTableView release];
    
}

// tableview两个协议方法
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return self.teamArr.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    
    static NSString *reuse = @"reuse";
    TeamsCell *cell = [tableView dequeueReusableCellWithIdentifier:reuse];
    if (!cell) {
        cell = [[[TeamsCell alloc] initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:reuse]autorelease];
    }
    
    TeamModel *teamModel = self.teamArr[indexPath.row];
    cell.teamTitle.text = teamModel.titel;
    cell.teamDes.text = teamModel.des;
    // 英雄1头像
    NSString *png1 = [NSString stringWithFormat:@"http://img.lolbox.duowan.com/champions/%@_120x120.jpg", teamModel.hero1];
    NSURL *url1 = [NSURL URLWithString:png1];
    [cell.imagHero1 sd_setImageWithURL:url1];
    // 英雄2头像
    NSString *png2 = [NSString stringWithFormat:@"http://img.lolbox.duowan.com/champions/%@_120x120.jpg", teamModel.hero2];
    NSURL *url2 = [NSURL URLWithString:png2];
    [cell.imagHero2 sd_setImageWithURL:url2];
    // 英雄3头像
    NSString *png3 = [NSString stringWithFormat:@"http://img.lolbox.duowan.com/champions/%@_120x120.jpg", teamModel.hero3];
    NSURL *url3 = [NSURL URLWithString:png3];
    [cell.imagHero3 sd_setImageWithURL:url3];
    // 英雄4头像
    NSString *png4 = [NSString stringWithFormat:@"http://img.lolbox.duowan.com/champions/%@_120x120.jpg", teamModel.hero4];
    NSURL *url4 = [NSURL URLWithString:png4];
    [cell.imagHero4 sd_setImageWithURL:url4];
    // 英雄5头像
    NSString *png5 = [NSString stringWithFormat:@"http://img.lolbox.duowan.com/champions/%@_120x120.jpg", teamModel.hero5];
    NSURL *url5 = [NSURL URLWithString:png5];
    [cell.imagHero5 sd_setImageWithURL:url5];
    
    return cell;
    
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    
    return 143;
    
}

// 点击事件
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    DetailTeamViewController *detailTeam = [[DetailTeamViewController alloc] init];
    TeamModel *teamModel = self.teamArr[indexPath.row];
    detailTeam.teamTitle = teamModel.titel;
    detailTeam.teamHero1 = teamModel.hero1;
    detailTeam.teamHero2 = teamModel.hero2;
    detailTeam.teamHero3 = teamModel.hero3;
    detailTeam.teamHero4 = teamModel.hero4;
    detailTeam.teamHero5 = teamModel.hero5;
    detailTeam.teamDes = teamModel.des;
    detailTeam.teamDes1 = teamModel.des1;
    detailTeam.teamDes2 = teamModel.des2;
    detailTeam.teamDes3 = teamModel.des3;
    detailTeam.teamDes4 = teamModel.des4;
    detailTeam.teamDes5 = teamModel.des5;
    
    [self.navigationController pushViewController:detailTeam animated:YES];
    
}




- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
